package com.css.fxfzmh.base.attachment.repository.impl;

import com.css.fxfzmh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzmh.base.attachment.repository.AttachmentInfoRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Dbj
 * @date 2022年08月02日  16:21
 */
@Repository
public class AttachmentInfoRepositoryImpl implements AttachmentInfoRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public int save(AttachmentInfoEntity attachmentInfoEntity) {
        StringBuilder sql = new StringBuilder();

        sql.append("insert into sys_attachment_info values(:attachId , :attachName, :attachPath, :isCompleted" +
                ", :isValid , :fileDirId )");
        SqlParameterSource source = new BeanPropertySqlParameterSource(attachmentInfoEntity);
        //拿到新插入的主键值（i）
            /*KeyHolder keyHolder = new GeneratedKeyHolder();
            int i = namedParameterJdbcTemplate.update(sql.toString(), source, keyHolder);*/
        int row = namedParameterJdbcTemplate.update(sql.toString(), source);
        return row==1?1:0;
    }

    @Override
    public AttachmentInfoEntity getAttach(String attachId) {
        String sql = "select * from sys_attachment_info where attach_id = '"+ attachId  + "' and is_valid = '1'";
        try {
            AttachmentInfoEntity attachmentInfoEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AttachmentInfoEntity.class));
            return attachmentInfoEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public boolean deleteFile(String attachId) {
        String sql = " update sys_attachment_info set is_valid = '0' where attach_id = ?";
        return jdbcTemplate.update(sql, attachId) > 0;
    }

    @Override
    public AttachmentInfoEntity queryFile(String fileId) {
        try {
            String sql = "select * from sys_attachment_info where is_valid = '1'and attach_id = ?";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AttachmentInfoEntity.class), fileId);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void update(AttachmentInfoEntity attach) {
        String sql = "update sys_attachment_info set " +
                "attach_name = :attachName," +
                "attach_path = :attachPath," +
                "is_completed = :isCompleted," +
                "is_valid = :isValid," +
                "file_dir_id = :fileDirId" +
                " where attach_id = :attachId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(attach);
        namedParameterJdbcTemplate.update(sql, source);
    }
}
